package org.itboys.shop.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.apache.ibatis.annotations.Param;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.tools.ResponseManager;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.product.dto.CommonTouristInfoDTO;
import org.itboys.product.entity.CommonTouristInfo;
import org.itboys.product.service.CommonTouristInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by DELL on 2016/7/6.
 */
@RestController
@RequestMapping("wx/info")
public class WXTouristInfoController extends BaseController{

    @Autowired
    private CommonTouristInfoService infoService;

    //新增或者修改旅客信息 dfdfd
    @RequestMapping("/save")
    public void save(
    		@Param("touristId")Long touristId, 
    		@Param("memberId")Long memberId, 
    		@Param("name")String name, 
    		@Param("mobile")String mobile, 
    		@Param("idType")Integer idType,
    		@Param("idNumber")String idNumber,
    		@Param("type")Integer type,
    		HttpServletResponse response, 
    		HttpServletRequest request, Model model) {
        try {
            Member member = (Member)request.getSession().getAttribute("user");
            infoService.save(touristId, member.getId(), name, mobile,idType, idNumber,type);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @RequestMapping("/detail")
    public void findInfo(@RequestParam long id, HttpServletResponse response, HttpServletRequest request){
        try{
            Member member = (Member)request.getSession().getAttribute("user");
            CommonTouristInfoDTO address = infoService.findById(member.getId());
//            CommonTouristInfoDTO address = infoService.findById(id);
            AjaxUtils.renderJson(response, address);
        } catch (Exception e) {
            logger.error(" ",e);
            ResponseManager.SystemError(response, null);
        }
    }

    //显示旅客信息
    @RequestMapping("/show")
    public void address(HttpServletResponse response, HttpServletRequest request, Model model) {
        try {
        	Member member = (Member)request.getSession().getAttribute("user");
            Map<String, Object> param= Maps.newHashMap();
            param.put("memberId",member.getId());
            param.put("isDeleted in", new Integer[]{0,2});
            List<CommonTouristInfo> infos = infoService.list(param);
            List<CommonTouristInfoDTO> infoResults = Lists.newArrayList();
            for (CommonTouristInfo info : infos) {
                CommonTouristInfoDTO dto = new CommonTouristInfoDTO(info);
                infoResults.add(dto);
            }
            AjaxUtils.renderJson(response, infoResults);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseManager.SystemError(response, null);
        }
    }
    //删除旅客信息
    @RequestMapping("/delete")
    public void deleteAddress(@RequestParam long id, HttpServletRequest request, HttpServletResponse response) {
        try {
            infoService.deleteInfo(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }


    //设置默认旅客信息
    @RequestMapping("/default")
    public void setDefault(@RequestParam long id, HttpServletRequest request, HttpServletResponse response) {
        try {
            Member member = (Member)request.getSession().getAttribute("user");
            infoService.setDefaultInfo(member.getId(), id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    @RequestMapping("/toEdit")
    public ModelAndView toEdit(
    		@Param("id") Long id,
    		@Param("callback") String callback, 
    		Model model,
    		HttpServletRequest request, HttpServletResponse response) {
        try {
//        	CommonTouristInfoDTO dto=new CommonTouristInfoDTO(infoService.getById(id));
        	model.addAttribute("info", infoService.getById(id));
        	model.addAttribute("callback", callback);
            return new ModelAndView("/order/add_info");
        } catch (Exception e) {
           logger.error("",e);
           return null;
        }
    }
}
